home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir30 / alsps.zip / ALSP4.DOC < prev    next >
Lisp/Scheme  |  1993-11-04  |  12KB  |  360 lines

  1. This is Lesson 4 of a series of AutoLISP training exercises given
  2. on the CompuServe ADESK Forum by the Autodesk, Inc. Training
  3. Department.
  4.  
  5. AUTOLISP: THE AUTOCAD CALCULATOR
  6.  
  7. AutoCAD does not have a Sidekick-style pop-up calculator
  8. to perform arithmetic operations and return the appropriate value
  9. to an active AutoCAD command prompt, but it has an entire
  10. programming language to calculate and store values and pass them
  11. to AutoCAD.  The comment that "AutoCAD doesn't have a built-in
  12. calculator" is as true as it is meaningless, because it has an
  13. entire built-in programming language, instead!
  14.  
  15. AutoLISP can be used at any time in the drawing editor.  It can
  16. be used at the command prompt, at the dimension prompt, and at
  17. any prompt generated by any AutoCAD command.  Its syntax is
  18. probably not as easy to master initially as that of a simple
  19. 10-key calculator; however, it is much more powerful.  And the
  20. syntax, once learned, is simple and consistent.
  21.  
  22. RETURNING A VALUE TO THE COMMAND PROMPT
  23.  
  24. The value of an AutoLISP expression is passed from AutoLISP to
  25. AutoCAD as the response to the active AutoCAD prompt.
  26.  
  27. If the command prompt is active, the value of an expression is
  28. simply printed in the command prompt area.  Any side effect that
  29. occurs during the expression, such as a new symbol binding, is
  30. irrelevant to the command prompt (as long as an AutoCAD command
  31. is not called in the expression).  For example, this form (a form
  32. being any complex expression that is treated as program code
  33. rather than data) binds the symbol X to the result of adding 4
  34. and 6 and then passes the value 10 to AutoCAD, which simply
  35. prints the result in the command prompt area.
  36.  
  37.     Command: (setq x (+ 4 6))
  38.     10
  39.  
  40. There's no requirement to bind a symbol.  Any form can be
  41. evaluated at the command prompt simply to return a calculated
  42. numeric value.
  43.  
  44. For example, this form returns the value of dividing the square
  45. root of 50 by the real number 9.
  46.  
  47.     Command: (/ (sqrt 50.0) 9.0)
  48.     0.785674
  49.  
  50. This form returns the value of multiplying the result of adding
  51. 2.5 and 6.3 by 24.0.
  52.  
  53.     Command: (* (+ 2.5 6.3) 24.0)
  54.     211.2
  55.  
  56.  
  57. RETURNING A VALUE TO AN AUTOCAD COMMAND
  58.  
  59. If an AutoCAD command is active, then evaluation of an AutoLISP form
  60. returns the value to AutoCAD.  The value becomes the response to
  61. the current prompt of the AutoCAD command.
  62.  
  63. In this example, the ARRAY command receives as the number of
  64. items to array the result of an AutoLISP form that divides 6.0
  65. by 0.35; in other words, we don't know offhand how many items
  66. will fit at 0.35 unit increments into a distance of 6.0 units, so
  67. we ask AutoLISP to calculate the value for us and return the
  68. answer to AutoCAD.  Since the prompt from ARRAY accepts only
  69. integer values rather than real numbers, we'll use the AutoLISP
  70. (fix <real>) function to round down to the nearest integer.
  71.  
  72.     Command: (/ 6.0 0.35)
  73.     17.1429
  74.  
  75.     Command: (fix 17.1429)
  76.     17
  77.  
  78.     Command: (fix (/ 6.0 0.35))
  79.     17
  80.  
  81.     Command: LINE
  82.     From point: 1,1
  83.     To point: 5,5
  84.     To point: <return>
  85.  
  86.     Command: ARRAY
  87.     Select objects: L
  88.     Select objects: <return>
  89.     Rectangular or Polar array (R/P): R
  90.     Number of rows (---) <1>: 1
  91.     Number of columns (|||) <1>: (fix (/ 6.0 0.35))
  92.     Distance between columns: 0.35
  93.  
  94. In this case, AutoCAD creates 17 items in the array.
  95.  
  96. The important point to consider is that an AutoLISP form has been
  97. used interactively, in the middle of an AutoCAD command, to
  98. calculate a value (in this case, an integer) that is subsequently
  99. and automatically given to the command at that particular point
  100. in its prompting sequence.
  101.  
  102. In this example, AutoLISP calculates the size of a fillet radius.
  103.  
  104.     Command: FILLET
  105.     Polyline/Radius/<Select two lines>: R
  106.     Enter fillet radius <1.0>: (/ 28.0 8.0)
  107.  
  108. Since both arguments to (/) are reals, the result is a real, and
  109. the size of the fillet radius is set to 3.5.
  110.  
  111. INTERACTIVE CALCULATION
  112.  
  113. Another method to pass values to an AutoCAD command is to use an
  114. AutoLISP form that itself prompts the user for an arbitrary set
  115. of values, and then performs some calculation on the values,
  116. eventually returning the answer to AutoCAD.  This is probably
  117. easier to use than it is to explain.
  118.  
  119. Before we perform some complex calculation with user-prompted
  120. values, we'll introduce two new AutoLISP functions: (getdist) and
  121. (getpoint).
  122.  
  123. TWO NEW INTERACTIVE FUNCTIONS
  124.  
  125. (getdist) halts the execution of a form and allows the user to
  126. specify a distance by typing in the value, or by selecting two
  127. points; this is exactly the same mechanism used by every AutoCAD
  128. prompt that calls for the user to specify a distance.
  129.  
  130. (getpoint) halts a form and allows the user to select a point,
  131. using any of the standard point selection methods: screen
  132. pointing, absolute, relative, or relative polar coordinate entry,
  133. object snaps, coordinate filters, or lastpoint specification.
  134.  
  135. GETDIST
  136.  
  137. This example uses an optional prompt to the (getdist) function,
  138. which prompts the user to enter a distance.  Try using this form
  139. at the command prompt and respond to it once by typing in a
  140. value, and again by picking two points on the display.
  141.  
  142. The control characters "\n", or backslash and lower-case n, mean
  143. newline.  They make sure the rest of the prompt appears on a new
  144. line in the command prompt area.
  145.  
  146.     Command: (getdist "\nEnter a distance: ")
  147.     Enter a distance: <enter a number>
  148.  
  149.     Command: (getdist "\nEnter a distance: ")
  150.     Enter a distance: <pick two points>
  151.  
  152. In both cases, (getdist) returns the distance entered by the user
  153. as a real number.
  154.  
  155. GETPOINT
  156.  
  157. This example uses an optional prompt to the (getpoint) function,
  158. which prompts the user to pick a point.  Try using this form with
  159. absolute coordinates, screen pointing, and object snaps.
  160.  
  161.     Command: (getpoint "\nPoint: ")
  162.     Point: 1,1
  163.     (1.0 1.0 0.0)
  164.  
  165. AutoLISP returns the point as a list of three real numbers.  3-D
  166. points are returned as a list of three reals; 2-D points as a
  167. list of two reals.  By default, all points chosen in Release 10
  168. are fully 3-D, with X, Y, and Z values.
  169.  
  170. The first element in a point list is the X value, the second the
  171. Y value, and the third the Z value.
  172.  
  173. This expression (1.0 1.0 0.0) is not a form, because it cannot be
  174. evaluated.  It contains no function as the first element of the
  175. list.  This is an example of AutoLISP using a list to store data
  176. rather than program code.
  177.  
  178.     Command: (getpoint "\nPoint: ")
  179.     Point: <pick point>
  180.  
  181.     Command: LINE
  182.     From point: 1,1
  183.     To point: 5,5
  184.     To point: <return>
  185.  
  186.     Command: (getpoint "\nPoint: ")
  187.     Point: MID
  188.     of <pick line>
  189.     (3.0 3.0 0.0)
  190.  
  191. PROMPTS ARE OPTIONAL
  192.  
  193. There's no law that says you have to include an optional string
  194. argument to (getdist) or (getpoint) as a prompt for the user.
  195. However, it's good practice to do so.  Can you guess why?
  196.  
  197. Try using (getdist) and (getpoint) without the optional string
  198. argument.
  199.  
  200.     Command: (getdist)
  201.     ?
  202.  
  203.     Command: (getpoint)
  204.     ?
  205.  
  206. SOME MORE CALCULATIONS
  207.  
  208. Computations might be a more accurate word, since a computer is
  209. essentially a calculator with memory, and these calculations
  210. require AutoLISP to remember distances and points.
  211.  
  212. In this example, variable X is bound to the list of three real
  213. numbers that form the coordinate 1,1,0.
  214.  
  215.     Command: (setq x (getpoint "\nPoint: "))
  216.     Point: 1,1
  217.     (1.0 1.0 0.0)
  218.  
  219. There's another way to bind X to a list of three real numbers
  220. without using the (getpoint) function.  Can you remember what it
  221. is?  It involves a function that tells AutoLISP to take the next
  222. argument at face value (data) rather than to try to evaluate it
  223. (a form)...
  224.  
  225. In this example, pick an arbitrary point anywhere on the screen
  226. with the cursor.
  227.  
  228.     Command: (setq x (getpoint))
  229.     <pick a point>
  230.  
  231. Next, draw a line starting at 0,0 and ending at the point stored
  232. in variable X.  How can you ask AutoLISP to use the current value
  233. of a variable?
  234.  
  235.     Command: LINE
  236.     From point: 0,0
  237.     To point: !x
  238.     To point: <return>
  239.  
  240. Binding variables to points is easy.  It allows the user to store
  241. as many points as needed by name in the current drawing editor
  242. session, and refer to them by name anytime AutoCAD asks for a
  243. point.
  244.  
  245.     Command: CIRCLE
  246.     3P/2P/TTR/<Center point>: !x
  247.     Diameter/<Radius>: 1.0
  248.  
  249.     Command: (setq origin (quote (0.0 0.0 0.0)))
  250.     (0.0 0.0 0.0)
  251.  
  252.     Command: MOVE
  253.     Select objects: Last
  254.     Select objects: <return>
  255.     Base point or displacement: !x
  256.     Second point of displacement: !origin
  257.  
  258. The previous example should have moved the circle and placed its
  259. center point at 0,0.  Can you see why?
  260.  
  261. Here's another example of the ARRAY command.  This time, AutoLISP
  262. will calculate the distance between two points and use that to
  263. help determine how many duplicate items it will create.
  264.  
  265. First, we'll build some arbitrary construction geometry.
  266.  
  267.     Command: LINE
  268.     From point: 1.1143,5
  269.     To point: 8.9178,5
  270.     To point: <return>
  271.  
  272.     Command: CIRCLE
  273.     3P/2P/TTR/<Center point>: 1.1143,5
  274.     Diameter/<Radius>: 0.5
  275.  
  276. Now, we'll use (getdist) to help provide facility to ARRAY that,
  277. on the face of it, ARRAY appears not to have: interactively
  278. calculate the number of items that must appear in the array based
  279. only on the distance between each item and total distance in the
  280. array!  In this example, we'll space each item 0.5 units apart.
  281.  
  282.     Command: ARRAY
  283.     Select objects: Last
  284.     Select objects: <return>
  285.     Rectangular or Polar array (R/P): R
  286.     Number of rows: 1
  287.     Number of columns: (fix (/ (getdist "\nDistance: ") 0.5))
  288.     Distance: END
  289.     of <pick start point of line>
  290.     Second point: END
  291.     of <pick end point of line>
  292.     Distance between columns: 0.5
  293.  
  294. Here's another example that fits a user-specified number of items
  295. to a calculated distance.
  296.  
  297.     Command: UNDO
  298.     Auto/Back/Control/End/Group/Mark/<number>: 1
  299.  
  300.     Command: ARRAY
  301.     Select objects: Last
  302.     Select objects: <return>
  303.     Rectangular or Polar array (R/P): R
  304.     Number of rows: 1
  305.     Number of columns: 10
  306.     Distance between columns: (/ (getdist) 10)
  307.     END
  308.     of <pick start point of line>
  309.     Second point: END
  310.     of <pick end point of line>
  311.     
  312. Next week: Writing New AutoCAD Commands
  313.  
  314. There are no questions for this week's lesson.  Please experiment
  315. on your own, using AutoLISP expressions at the command prompt and
  316. within AutoCAD commands to calculate and store answers for AutoCAD.
  317.  
  318. EXTRA CREDIT
  319.  
  320. One of the most interesting forms that comes with Release 10 is
  321. the file REF.LSP, which you'll find on the Support Disks.  It's
  322. documented in Appendix A of the AutoLISP Programmer's Reference.
  323.  
  324. Examine the form in REF.LSP.  See if you can determine how it
  325. works!  If you can, then you have a clear understanding of how
  326. AutoCAD and AutoLISP work with each other.  Clue #1: the (setvar)
  327. function does the same thing as the AutoCAD SETVAR command.
  328.  
  329. ANSWERS TO LESSON 3
  330.  
  331. 1.  Reals are stored as double precision floating point numbers,
  332. accurate to at least fourteen significant digits of precision.
  333.  
  334. 2.  No.
  335.  
  336. 3.  For code compatibility with DOS versions of AutoCAD.
  337.  
  338. 4.  The file cannot be accessed for read, write, or append
  339. operations, nor can it be closed (AutoCAD itself will close the
  340. file on exit from the drawing editor).
  341.  
  342. 5.  Selection-sets occupy temporary file slots, and as such
  343. should be treated as precious resources.  A variable should
  344. always be bound to a newly-created selection-set, else it cannot
  345. be accessed by a later operation within AutoLISP.
  346.  
  347. 6.  Up to six; however, don't use more than four unless you have
  348. to.
  349.  
  350. 7.  A pointer to an entity record in the drawing editor session.
  351.  
  352. 8.  It can't; however, entity handles can be stored across
  353. editing sessions and used to retrieve the same entities.
  354.  
  355. 9.  100 characters; the limit of available heap space that can be
  356. allocated as string space, in other words, the limit of available
  357. memory.
  358.  
  359. 10. The (type) function.
  360.